package com.totcy.atom.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.totcy.atom.MyApplication;
import com.totcy.atom.bean.User;
import com.totcy.baselibrary.utils.SPUtil;

public class UserUtil {

    private static String SP_USER_INFO = "user_info";
    private static UserUtil instances;
    private Gson gson = new Gson();
    private User user = null;

    public static UserUtil getInstances() {

        if (instances == null) {
            synchronized (UserUtil.class) {
                if (instances == null) {
                    instances = new UserUtil();
                }
            }

        }
        return instances;
    }

    public void saveUserInfo(User user) {
        String json = gson.toJson(user);
        this.user = user;
        SPUtil.put(MyApplication.getApplication(), SP_USER_INFO, json);
    }

    public void removeUserInfo() {
        user = null;
        SPUtil.remove(MyApplication.getApplication(), SP_USER_INFO);
    }

    public User getUserInfo() {
        if (user == null) {
            String json = (String) SPUtil.get(MyApplication.getApplication(), SP_USER_INFO, "");
            if (!TextUtils.isEmpty(json)) {
                user = gson.fromJson(json, User.class);
            }
        }
        return user;
    }

    public String getUserName() {
        return getUserInfo() != null ? getUserInfo().getUserName() : "";
    }

    public String getUserToken() {
        return getUserInfo() != null ? getUserInfo().getToken() : "";
    }

}
